<template>
  <div class="addGoods">
    <el-container>
      <el-header>
        <el-page-header @Back="$router.replace('/personinfomation')">
          <template #content>
            <div class="flex items-center">
              <span class="text-large font-600 mr-3">管理商品</span>
              &nbsp;
              <el-tag>商家</el-tag>
            </div>
          </template>
          <template #extra>
            <div class="flex items-center">
              <el-button type="primary" class="goods-position" @click="addGoods">
                添加商品
              </el-button>
            </div>
          </template>
        </el-page-header>
      </el-header>
      <el-main>
        <el-scrollbar v-if="goodsList.length !== 0" height="500px">
          <GoodsManagement
            v-for="(item, index) in goodsList"
            :key="index"
            :item="item"
            @updateItem="updateGoods(item.id)"
            @deleteItemSuccess="getAllGoods"></GoodsManagement>
        </el-scrollbar>
        <el-empty
          v-if="goodsList.length == 0"
          style="height: 550px"
          description="您的商店还没有商品" />
        <!-- 添加商品弹层 -->
        <GoodsAddDrawer
          ref="dialog"
          @addSuccess="getAllGoods"
          @updateSuccess="getAllGoods"></GoodsAddDrawer>
      </el-main>
    </el-container>
  </div>
</template>

<script setup>
import GoodsManagement from '@/components/GoodsManagement.vue'
import GoodsAddDrawer from '@/components/GoodsAddDrawer.vue'
import { ref } from 'vue'
import { getAllGoodsInfo } from '@/api/goods'
import { useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'

const route = useRoute()

// 抽屉对象
const dialog = ref()

// 商品列表
const goodsList = ref([])

// 获取所有商品
const getAllGoods = async () => {
  const res = (await getAllGoodsInfo(route.params.id)).data
  if (res.code == 505) {
    ElMessage.error(res.msg)
    return
  }
  goodsList.value = res.data
}

getAllGoods()

// 添加物品
const addGoods = () => {
  dialog.value.open()
}

// 修改物品信息
const updateGoods = (goodId) => {
  dialog.value.open(goodId)
}
</script>

<style scoped>
.el-container {
  height: 700px;
  background-color: white;
  padding: 15px 0 0 10px;
}
</style>
